
;
(function($, $$) {
    $.fn.buttonSet = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.ButtonSet);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.ButtonSet = function(self, options) {
        if (!(this instanceof $$.ButtonSet)) {
            return new $$.ButtonSet(self, options);
        }
        var defaults = {

            buttonSetClass : $$.defaultBoxShadow + " uiCornerAll",

            buttonSetType : null,

            buttonSetGroup : null,

            buttonClass : $$.defaultGradientBG,

            buttonBorderClass : "uiGreyBorder",

            buttonSetBorderClass : $$.defaultBorder,

            buttonHoverClass : $$.hoverGradientBG,

            buttonSelectedClass : $$.selectedGradientBG + " uiActive",

            buttonPressClass : $$.hoverGradientBG + " uiButtonPressed",

            buttonWidth : null,

            buttonMargin : null,

            iconArray : null,

            iconFloatArray : null,

            defaultSelectedItem : null,

            vertical : false,


            statusClass : null,

            imgIconHTML : '<div class="uiIcon uiUncheckedButtonIcon"></div>',


            click : null
        };
        var opts = $.extend(defaults, options);
        $$.ButtonSet.prototype.superClass.call(this, self, opts);
        this.init();
    };

    $$.inherit($$.ButtonSet, $$.Widget, {

        init : function() {
            var buttonSetObj = this;
            buttonSetObj.createWidget();
            initButtonSet(buttonSetObj.jqObj, buttonSetObj.opts);
            return buttonSetObj;
        },

        clickButton : function(index) {
            var buttonSetObj = this, self = buttonSetObj.jqObj, opts = buttonSetObj.opts;
            index = index || 0;
            var obj = self.children().eq(index).click();
            return buttonSetObj;
        },

        button : function(index) {
            var buttonSetObj = this, self = buttonSetObj.jqObj, opts = buttonSetObj.opts;
            index = index || 0;
            return self.children().eq(index);
        },

        buttonText : function(index, text) {
            var buttonSetObj = this, self = buttonSetObj.jqObj, opts = buttonSetObj.opts;
            index = index || 0;
            var obj = self.children().eq(index);
            if (text === undefined) {
                return obj.text();
            }
            var iconObj = obj.children(".uiIcon");
            obj.html(text).prepend(iconObj);
            return buttonSetObj;
        },

        buttonIcon : function(index, icon) {
            var buttonSetObj = this, self = buttonSetObj.jqObj, opts = buttonSetObj.opts;
            index = index || 0;
            if (!$.isArray(opts.iconArray)) {
                return "";
            }
            var obj = self.children().eq(index);
            var arrayIndex = opts.iconArray.length - 1;
            var iconClass;
            if (index > arrayIndex) {
                index = arrayIndex;
            }
            iconClass = opts.iconArray[index];
            if (icon === undefined) {
                return iconClass;
            }
            obj.children("span").removeClass(iconClass).addClass(icon);
            opts.iconArray[index] = icon;
            return buttonSetObj;
        },

        val : function(index, checked) {
            var buttonSetObj = this, self = buttonSetObj.jqObj, opts = buttonSetObj.opts;
            index = index || 0;
            var obj = self.children().eq(index);
            var hasStatus = false;
            $.each("uiCheckBox uiRadio uiImgRadio uiImgCheckBox".split(" "), function(n, value) {
                if (obj.hasClass(value)) {
                    hasStatus = true;
                    return false;
                }
            });
            if (!hasStatus) {
                return false;
            }
            if (checked === undefined) {
                if (obj.hasClass(opts.buttonSelectedClass)) {
                    return true;
                }
                return false;
            }
            if (checked) {
                if (!obj.hasClass(opts.buttonSelectedClass)) {
                    obj.trigger("click.uiButtonSet");
                }
            }
            else {
                if (obj.hasClass(opts.buttonSelectedClass)) {
                    obj.removeClass(opts.buttonSelectedClass).addClass(opts.buttonClass);
                }
            }
            return buttonSetObj;
        },

        removeButton : function(index) {
            var buttonSetObj = this, self = buttonSetObj.jqObj, opts = buttonSetObj.opts;
            index = index || 0;
            if ($.isArray(opts.iconArray)) {
                opts.iconArray.splice(index, 1);
            }
            return self.children(".uiButton").eq(index).remove();
        }
    });

    function initButtonSet(self, opts) {

        opts.statusClass = {};
        var buttonTypeClass;
        var groupStr = "";
        switch (opts.buttonSetType) {
            case "radio":
                buttonTypeClass = "uiRadio";
                groupStr = $$.getRandomKey(5);
                break;
            case "checkBox":
                buttonTypeClass = "uiCheckBox";
                break;
            case "imageRadio":
                buttonTypeClass = "uiImgRadio";
                groupStr = $$.getRandomKey(5);
                break;
            case "imageCheckBox":
                buttonTypeClass = "uiImgCheckBox";
                break;
            default:
                buttonTypeClass = "";
                break;
        }
        self.addClass("uiButtonSet uiWidget uiNoSelectText").children().each(function(n) {
            var obj = $(this).addClass(buttonTypeClass), buttonClass;
            if (groupStr.length != 0) {
                obj.attr("group", groupStr);
            }
            if (opts.buttonMargin) {
                buttonClass = "uiButton uiCornerAll " + opts.buttonClass + " " + opts.buttonBorderClass;
            }
            else {
                buttonClass = "uiButton " + opts.buttonClass;
            }

            if (obj.hasClass("uiImgRadio") || obj.hasClass("uiImgCheckBox")) {
                obj.wrapInner('<span />').prepend(opts.imgIconHTML);
            }
            if (n !== 0) {
                if (opts.buttonMargin) {
                    if (opts.vertical) {
                        obj.css("margin-top", opts.buttonMargin);
                    }
                    else {
                        obj.css("margin-left", opts.buttonMargin);
                    }
                }
                else {
                    if (opts.vertical) {
                        buttonClass += (" uiBorderTop " + opts.buttonBorderClass);
                    }
                    else {
                        buttonClass += (" uiBorderLeft " + opts.buttonBorderClass);
                    }
                }
            }
            obj.addClass(buttonClass);
            if ($.isArray(opts.iconArray)) {
                var index = n;
                var floatClass = "";
                if (index >= opts.iconArray.length) {
                    index = opts.iconArray.length - 1;
                }
                if ($.isArray(opts.iconFloatArray)) {
                    if (n >= opts.iconFloatArray.length) {
                        floatClass = opts.iconFloatArray[opts.iconFloatArray.length - 1];
                    }
                    else {
                        floatClass = opts.iconFloatArray[n];
                    }
                    obj.wrapInner('<span />');
                    if (floatClass === "right") {
                        floatClass = "uiIconFloatRight";
                    }
                    else {
                        floatClass = "";
                    }

                }
                obj.wrapInner('<span />').prepend('<span class="uiIcon ' + opts.iconArray[index] + " " + floatClass + '"></span>');
            }
            if (opts.buttonWidth) {
                obj.width(opts.buttonWidth);
            }
        });
        if (opts.buttonMargin === null) {
            self.addClass(opts.buttonSetClass + " " + opts.buttonSetBorderClass);
        }
        if (opts.vertical) {
            self.width(self.children().outerWidth(true));
        }

        initEvent(self, opts);
        if (opts.disabled) {
        }
        if (opts.defaultSelectedItem !== null) {
            changeButtonStatus(self.children().eq(opts.defaultSelectedItem), opts);
        }
    }


    function initEvent(self, opts) {
        var mouseDownFlag = false;
        self.children(".uiButton, .uiImgButton").bind("mouseenter.uiButtonSet mouseleave.uiButtonSet mousedown.uiButtonSet mouseup.uiButtonSet click.uiButtonSet", function(e) {
            if (opts.disabled) {
                return false;
            }
            if (e.type === "click") {
                if ($.isFunction(opts.click)) {
                    if (opts.click.call(self[0], $(this), e) === false) {
                        return self;
                    }
                }
                changeButtonStatus($(this), opts);
                return;
            }
            var classValue;
            var funcValue;
            var obj = $(this);
            switch (e.type) {
                case "mouseenter":


                    setStatusClass(obj, opts, opts.buttonHoverClass);
                    break;
                case "mouseleave":


                    removeStatusClass(obj, opts, opts.buttonHoverClass);
                    break;
                case "mousedown":


                    setStatusClass(obj, opts, opts.buttonPressClass);
                    break;
                case "mouseup":


                    removeStatusClass(obj, opts, opts.buttonPressClass);
                    break;
            }

        });
    }


    function changeButtonStatus(obj, opts) {
        var group;
        if (obj.hasClass("uiRadio") || obj.hasClass("uiImgRadio")) {
            group = obj.attr("group");
            var siblingsObj = obj.siblings('[group="' + group + '"]');
            obj.removeClass(opts.buttonHoverClass + " " + opts.buttonClass).addClass(opts.buttonSelectedClass);
            siblingsObj.removeClass(opts.buttonSelectedClass).addClass(opts.buttonClass);
            if (obj.hasClass("uiImgRadio")) {
                obj.addClass("uiImgRadioChecked").children(".uiIcon").removeClass("uiUnCheckedButtonIcon").addClass("uiCheckedButtonIcon");
                siblingsObj.removeClass("uiImgRadioChecked").children(".uiIcon").removeClass("uiCheckedButtonIcon").addClass("uiUnCheckedButtonIcon");
            }
        }
        else
        if (obj.hasClass("uiCheckBox") || obj.hasClass("uiImgCheckBox")) {
            if (opts.statusClass[opts.buttonHoverClass] === null) {
                obj.toggleClass(opts.buttonSelectedClass).toggleClass(opts.buttonClass);
            }
            else
            if (opts.statusClass[opts.buttonHoverClass] !== opts.buttonSelectedClass) {
                obj.removeClass(opts.buttonHoverClass).addClass(opts.buttonSelectedClass);
            }
            else {
                obj.removeClass(opts.buttonHoverClass).addClass(opts.buttonClass);
            }
            if (obj.hasClass("uiImgCheckBox")) {
                obj.toggleClass("uiImgCheckBoxChecked").children(".uiIcon").toggleClass("uiCheckedButtonIcon uiUnCheckedButtonIcon");
            }
        }
        else {
            obj.removeClass(opts.buttonHoverClass).addClass(opts.buttonClass);
        }
        $.each(opts.statusClass, function(key, value) {
            opts.statusClass[key] = null;
        });
    }


    function setOriginClass(obj, opts, statusClass) {
        $.each([opts.buttonClass, opts.buttonSelectedClass, opts.buttonHoverClass, opts.buttonPressClass], function(n, value) {
            if (obj.hasClass(value)) {
                opts.statusClass[statusClass] = value;
            }
            obj.removeClass(value);
        });
    }


    function getOriginClass(obj, opts, statusClass) {
        if (opts.statusClass[statusClass] === null) {
            return;
        }
        $.each([opts.buttonClass, opts.buttonSelectedClass, opts.buttonHoverClass, opts.buttonPressClass], function(n, value) {
            obj.removeClass(value);
        });
        obj.addClass(opts.statusClass[statusClass]);
    }


    function setStatusClass(obj, opts, statusClass) {
        setOriginClass(obj, opts, statusClass);
        obj.addClass(statusClass);
    }


    function removeStatusClass(obj, opts, statusClass) {
        getOriginClass(obj, opts, statusClass);
    }
})(jQuery, BASE);